package com.moarea.app.controller.system.dictionary;

import com.moarea.app.lang.model.ResultObject;
import com.moarea.app.model.BaseDictionary;
import com.moarea.app.mybatis.model.PagedObject;
import com.moarea.app.mybatis.model.ReqPagedObject;
import com.moarea.app.service.impl.dictionary.BaseDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/BaseDictionaryController")
public class BaseDictionaryController {

    @Autowired
    private BaseDictionaryService baseDictionaryService;

    /**
     * 根据父节点查询所有的子节点
     *
     * @param param   查询差数
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectByPcode", headers = {"Accept=application/json;charset=UTF-8"})
    @ResponseBody
    public ResultObject selectByPcode(@RequestBody Map<String, Object> param, HttpSession session) {
        ResultObject resultObject = ResultObject.createInstance();
        List<Map<String, Object>> list = baseDictionaryService.selectByPcode(param);
        resultObject.setData(list);
        return resultObject;
    }

    /**
     * 根据父节点查询所有的子节点，沒有code
     *
     * @param param   查询差数
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectByPcodeNoCode", headers = {"Accept=application/json;charset=UTF-8"})
    @ResponseBody
    public ResultObject selectByPcodeNoCode(@RequestBody Map<String, Object> param, HttpSession session) {
        ResultObject resultObject = ResultObject.createInstance();
        List<Map<String, Object>> list = baseDictionaryService.selectByPcodeNoCode(param);
        resultObject.setData(list);
        return resultObject;
    }

    /**
     * 根据ID查询所有的子节点
     *
     * @param param   查询差数
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectByID", headers = {"Accept=application/json;charset=UTF-8"})
    @ResponseBody
    public ResultObject selectByID(@RequestBody Map<String, Object> param, HttpSession session) {
        ResultObject resultObject = ResultObject.createInstance();
        List<Map<String, Object>> list = baseDictionaryService.selectByID(param);
        resultObject.setData(list);
        return resultObject;
    }


    /**
     * 查询数据字典所有数据
     *
     * @return
     */
    @RequestMapping(value = "/listTreeMenus", headers = {"Accept=application/json;charset=UTF-8"})
    @ResponseBody
    public ResultObject listTreeMenus(HttpSession session) {

        return baseDictionaryService.listTreeMenus(session);
    }


    /**
     * 根据前台传入值查询子节点PID
     *
     * @return
     */
    @RequestMapping(value = "/selectChildBypId", headers = {"Accept=application/json;charset=UTF-8"})
    @ResponseBody
    public PagedObject<List<BaseDictionary>> selectChildBypId(@RequestBody ReqPagedObject formValues, HttpSession session) {
        return baseDictionaryService.selectChildBypId(formValues.getData(), formValues.getPage(), formValues.getLimit(),
                formValues.getAllcount());

    }

    /**
     * 主界面新增
     */
    @RequestMapping(value = "/insert", headers = {"Accept=application/json;charset=UTF-8"})
    @ResponseBody
    public ResultObject insert(@RequestBody BaseDictionary param, HttpSession session) {
        ResultObject resultObject = ResultObject.createInstance();
        Map<String,Object>map=new HashMap<>();
        map.put("PID",param.getPID());
        map.put("PARACODE",param.getPARACODE());
        //查询是否编码是否重复
        int isRepeat=baseDictionaryService.isRepeatByPID(map);
        if (isRepeat!=0){
            resultObject.setMessage("同一节点编码不可重复");
            resultObject.setSuccess(false);
            return resultObject;
        }else {
            param.setID(UUID.randomUUID().toString());
            int result = baseDictionaryService.insert(param);

            if (result > 0) {
                //插入日志
                resultObject.setMessage("新增成功");
                resultObject.setSuccess(true);
                return resultObject;
            } else {
                resultObject.setMessage("新增失败");
                resultObject.setSuccess(false);
                return resultObject;
            }
        }
    }

    /**
     * 修改
     *
     * @param param
     * @param session
     * @return
     */
    @RequestMapping(value = "/update", headers = {"Accept=application/json;charset=UTF-8"})
    @ResponseBody
    public ResultObject update(@RequestBody BaseDictionary param, HttpSession session) {
        ResultObject resultObject = ResultObject.createInstance();

        Map<String,Object>map=new HashMap<>();
        map.put("ID",param.getID());
        map.put("PID",param.getPID());
        map.put("PARACODE",param.getPARACODE());
        //查询是否编码是否重复
        int isRepeat=baseDictionaryService.isRepeatByPIDAndID(map);
        if (isRepeat!=0){
            resultObject.setMessage("同一节点编码不可重复");
            resultObject.setSuccess(false);
            return resultObject;
        }else {
            int result = baseDictionaryService.getMapper().updateByPrimaryKeySelective(param);

            if (result > 0) {
                //插入日志
                resultObject.setMessage("修改成功");
                resultObject.setSuccess(true);
                return resultObject;
            } else {
                resultObject.setMessage("修改失败");
                resultObject.setSuccess(false);
                return resultObject;
            }
        }
    }


    /**
     * 修改
     *
     * @param param
     * @param session
     * @return
     */
    @RequestMapping(value = "/updateList", headers = {"Accept=application/json;charset=UTF-8"})
    @ResponseBody
    public ResultObject updateList(@RequestBody List<BaseDictionary> param, HttpSession session) {
        ResultObject resultObject = ResultObject.createInstance();
        if (param != null) {
            for (BaseDictionary baseDictionary : param) {
                baseDictionaryService.update(baseDictionary);
            }
            resultObject.setSuccess(true);
            resultObject.setMessage("保存成功");
        }
        return resultObject;
    }

    /***
     * 删除
     * @param param
     * @param session
     * @return
     */
    @RequestMapping(value = "/delete", headers = {"Accept=application/json;charset=UTF-8"})
    @ResponseBody
    public ResultObject delete(@RequestBody List<Map<String,Object>> param, HttpSession session) {
        ResultObject resultObject = ResultObject.createInstance();
        try {
            if (param != null && param.size() > 0) {
                for (Map baseData : param) {
                    baseDictionaryService.deleteByID(baseData.get("ID").toString(),session);

                }
            }

            resultObject.setMessage("删除成功");
            return resultObject;

        } catch (Exception e) {
            e.printStackTrace();
            resultObject.setMessage("删除失败");
            resultObject.setSuccess(false);
            return resultObject;
        }

    }

    @RequestMapping(value = "/selectMaxSeqByPID", headers = {"Accept=application/json;charset=UTF-8"})
    @ResponseBody
    public ResultObject selectMaxSeqByPID(@RequestBody Map<String, Object> param, HttpSession session) {
        return baseDictionaryService.selectMaxSeqByPID(param, session);
    }


    /**
     * 根据大类Pid查部类ID
     *
     * @param PID
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectCategoryByBigPID", headers = {"Accept=application/json;charset=UTF-8"})
    @ResponseBody
    public String selectCategoryByBigPID(@RequestBody String PID, HttpSession session) {

        return baseDictionaryService.selectCategoryByBigPID(PID, session);
    }

    /**
     * 根据父节点编号和子节点编号查询某个节点
     *
     * @param param
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectNodeBySupAndSubPcode", headers = {"Accept=application/json;charset=UTF-8"})
    @ResponseBody
    public ResultObject selectNodeBySupAndSubPcode(@RequestBody Map<String, Object> param, HttpSession session) {
        return baseDictionaryService.selectNodeBySupAndSubPcode(param, session);
    }
}
